class SqQueue {
    constructor(length) {
        this.queue = new Array(length + 1);
        this.first = 0;
        this.last = 0;
        this.size = 0;
    }
    enQueue(item) {
        if (this.first === (this.last + 1) % this.queue.length) {
            this.resize(this.getLength() * 2 + 1);
        }
        this.queue[this.last] = item;
        this.size++;
        this.last = (this.last + 1) % this.queue.length;
    }
    deQueue() {
        if (this.isEmpty()) {
            throw Error('Queue is empty');
        }
        let r = this.queue[this.first];
        this.queue[this.first] = null;
        this.first = (this.first + 1) % this.queue.length;
        this.size--;
        if (this.size === this.getLength() / 4 && this.getLength() / 2 !== 0) {
            this.resize(this.getLength() / 2);
        }
        return r;
    }
    getHeader() {
        if (this.isEmpty()) {
            throw Error('Queue is empty');
        }
        return this.queue[this.first];
    }
    getLength() {
        return this.queue.length - 1;
    }
    isEmpty() {
        return this.first === this.last;
    }
    resize(length) {
        let q = new Array(length);
        for (let i = 0; i < length; i++) {
            q[i] = this.queue[(i + this.first) % this.queue.length]
        }
        this.queue = q;
        this.first = 0;
        this.last = this.size;
    }
}